package com.unohacha.qqj.fzdr.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import android.content.res.Resources;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.adapter.BaseRVAdapter;
import com.unohacha.qqj.fzdr.adapter.BaseViewHolder;
import com.unohacha.qqj.fzdr.bean.find.DefaultFindBean;
import com.unohacha.qqj.fzdr.bean.find.PackUpLableBean;
import com.unohacha.qqj.fzdr.imp.MyCallBack;
import com.unohacha.qqj.fzdr.imp.UrlConfig;
import com.unohacha.qqj.fzdr.imp.XUtil;
import com.unohacha.qqj.fzdr.util.Prefer;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 发现界面的筛选弹窗
 */

public class FilterPopwindow extends PopupWindow{
    private OnPackUpListener packUpListener;
    private Context mContext;
    private Activity aty;
    private Resources resource;
    private List<PackUpLableBean.DataEntity> mPackUpLableList = new ArrayList<>();
    private BaseRVAdapter mBaseAdapter;

    public FilterPopwindow(Context context, Activity aty) {
        this.mContext = context;
        this.aty = aty;
        resource =  mContext.getResources();
    }

    //展示布局
    public void showUi(){
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.widget_filter_pop,null
        );

        RecyclerView mRecyclerView = view.findViewById(R.id.mRecyclerView);
        RelativeLayout pack_up_rl = view.findViewById(R.id.pack_up_rl);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,3, LinearLayout.VERTICAL,false));

        pack_up_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == packUpListener){
                    dismissView();
                }else {
                    packUpListener.onPackUp();
                    dismissView();
                }
            }
        });


        // 设置SelectPicPopupWindow的View
        this.setContentView ( view );
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth (WindowManager.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight (WindowManager.LayoutParams.WRAP_CONTENT);
        //设置动画
        this.setAnimationStyle(R.style.animationPop01);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        getPackUpData(mRecyclerView);

    }

    //获取筛选的数据
    private void getPackUpData(final RecyclerView mRecyclerView){

        Map<String,Object> params = new HashMap<>();
        params.put("typeid",2);//类型(1: 更多供求, 2: 供求列表)
        params.put("token",Prefer.getInstance().getToken());
        XUtil.Post(UrlConfig.PACK_UP_LABLE,params,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("code"))){
                        Gson gson = new Gson();
                        PackUpLableBean packUpLableBean = gson.fromJson(result,PackUpLableBean.class);
                        if (null != packUpLableBean){
                            if (0 != mPackUpLableList.size()){
                                mPackUpLableList.clear();
                            }

                            for (int i = 0; i < packUpLableBean.getData().size(); i++) {
                                PackUpLableBean.DataEntity dataEntity = packUpLableBean.getData().get(i);
                                if (Prefer.getInstance().getPackUpLable()
                                        == i) {
                                    dataEntity.setChoose(true);
                                }else {
                                    dataEntity.setChoose(false);
                                }
                                mPackUpLableList.add(dataEntity);
                            }

                            setMyAdapter(mRecyclerView);
                        }
                    }else {
                        Toast.makeText(mContext, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void setMyAdapter(RecyclerView mRecyclerView){
        mBaseAdapter = new BaseRVAdapter(mContext,mPackUpLableList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_pack_up;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                if (null != mPackUpLableList) {
                    final TextView item_text = holder.getTextView(R.id.item_text);
                    item_text.setText(mPackUpLableList.get(position).getName());
                    if (mPackUpLableList.get(position).isChoose()){
                        item_text.setTextColor(resource.getColor(R.color.app_green2));
                    }else {
                        item_text.setTextColor(resource.getColor(R.color.text_black_666));
                    }

                    item_text.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (null != mPackUpLableList){
                                if (null != packUpListener){
                                    for (int i = 0; i <mPackUpLableList.size() ; i++) {
                                        PackUpLableBean.DataEntity dataEntity = mPackUpLableList.get(i);
                                        if (position == i){
                                            dataEntity.setChoose(true);
                                        }else {
                                            dataEntity.setChoose(false);
                                        }
                                    }
                                    Prefer.getInstance().setPackUpLable(position);
                                    packUpListener.onGetPositionData(mPackUpLableList.get(position).getId(),
                                            mPackUpLableList.get(position).getName());
                                    dismissView();
                                }
                            }
                        }
                    });

                }
            }

        };


        mRecyclerView.setAdapter(mBaseAdapter);
        mBaseAdapter.notifyDataSetChanged();
    }

    public interface OnPackUpListener{
        void onPackUp();//收起的方法
        void onGetPositionData(int id,String name);
    }

    public void setOnPackUpListener(OnPackUpListener onPackUpListener) {
        this.packUpListener = onPackUpListener;
    }

    //关闭
    public void dismissView(){
        dismiss();
    }
}
